package org.libraq.hrm.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.apache.commons.lang.time.DateUtils;
import org.libraq.hrm.constant.BaseConstant;
import org.libraq.hrm.domain.Employee;
import org.libraq.hrm.domain.Tenant;
import org.libraq.hrm.exception.code.ErrorCode;
import org.libraq.hrm.feign.LoginFeignClient;
import org.libraq.hrm.mapper.EmployeeMapper;
import org.libraq.hrm.mapper.TenantMapper;
import org.libraq.hrm.service.ITenantService;
import org.libraq.hrm.to.LoginMealTo;
import org.libraq.hrm.to.LoginTo;
import org.libraq.hrm.to.TenantRegisterTo;
import org.libraq.hrm.util.AjaxResult;
import org.libraq.hrm.util.AssertUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author whale.chen
 * @since 2021-08-15
 */
@Service
public class TenantServiceImpl extends ServiceImpl<TenantMapper, Tenant> implements ITenantService {
    @Resource
    private TenantMapper tenantMapper;
    @Resource
    private LoginFeignClient loginFeignClient;
    @Resource
    private EmployeeMapper employeeMapper;
    @Value("${termOfValidity.day}")
    private int day;//yml中设置套餐有效期


    @Override
    @Transactional
    public void entering(TenantRegisterTo to) {
        //0 获取参数并校验
        Tenant tenant = to.getTenant();
        Employee employee = to.getEmployee();
        String username = to.getUsername();
        String password = to.getPassword();
        Long mealId = to.getMealId();
        check(tenant,employee);
        //1 保存Login信息
        LoginTo login = new LoginTo();
        login.setUsername(username);
        login.setPassword(password);
        login.setType(BaseConstant.LoginConstant.TYPE_TENANT);//设置登录类型：机构
        AjaxResult ajaxResult = loginFeignClient.save(login);
        AssertUtils.isNotNull(ajaxResult.getResultObj(),ErrorCode.OPER_FAIL.getMessage());//断言：login保存成功
        Long loginId = Long.valueOf(ajaxResult.getResultObj().toString());
        //2 保存员工
        employee.setRealName(to.getUsername());//注意前端传的是username 数据库employee表中叫做RealName
        Date now = new Date();
        employee.setInputTime(now);
        employee.setState(BaseConstant.EmployeeConstant.STATE_NORMAL);//设置员工状态：普通
        employee.setLoginId(loginId);
        employee.setType(BaseConstant.EmployeeConstant.TYPE_TENANT_ADMIN);//设置员工类型：机构管理员
        employeeMapper.insert(employee);
        //3 保存机构
        tenant.setAdminId(employee.getId());//emloyeeId绑定至tenant
        tenant.setRegisterTime(now);
        tenant.setState(BaseConstant.TenantConstant.STATE_TO_AUDIT);//设置机构状态：待审核
        this.insert(tenant);
        employee.setTenantId(tenant.getId());//tenantId绑定至employee
        employeeMapper.updateById(employee);
        //4 保存套餐
        LoginMealTo mealTo = new LoginMealTo();
        mealTo.setLoginId(loginId);
        mealTo.setMealId(mealId);
        mealTo.setExpireDate(DateUtils.addDays(now,day));//yml自定义有效期
        mealTo.setState(BaseConstant.MealConstant.STATE_TO_PAY);//设置套餐状态：待支付
        loginFeignClient.saveLoginMeal(mealTo);

    }

    private void check(Tenant tenant, Employee employee) {
        //断言：参数非空
        AssertUtils.isNotNull(tenant.getCompanyName(), ErrorCode.TENANT_NAME_NOT_NULL.getMessage());
        AssertUtils.isNotNull(tenant.getAddress(), ErrorCode.TENANT_ADDRESS_NOT_NULL.getMessage());
        AssertUtils.isNotNull(tenant.getCompanyNum(), ErrorCode.TENANT_ADDRESS_NOT_NULL.getMessage());
        //断言：信用码合法
        AssertUtils.isLicense(tenant.getCompanyNum(),ErrorCode.TENANT_CODE_ILLEGAL.getMessage());
        //断言：手机号合法
        AssertUtils.isMobile(employee.getTel(),ErrorCode.PHONE_ILLEGAL.getMessage());
        //断言：未重复注册
        AssertUtils.isFalse(tenantMapper.checkExistByCode(tenant.getCompanyNum()),ErrorCode.TENANT_IS_EXIST.getMessage());
        AssertUtils.isFalse(tenantMapper.checkExistByName(tenant.getCompanyName()),ErrorCode.TENANT_IS_EXIST.getMessage());
    }
}
